<?php

// check username & password

include "../map-config.php";

if($_POST['password'] != ADMIN_PASSWORD AND PASSWORD_BYPASS != true) {

?>

<p>Password: </p>
<form action="index.php" method="POST">
<input type="password" name="password" />
<input type="submit">
</form>

<?

}
else { 


?>

<h1>brad's mappy email search</h1>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<form name="" method="post" action="" id="">
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo GMAPS_API_KEY ?>" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var map;
function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));		
		map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());
		GEvent.addListener(map, "click", clickFunc);
		var lat = 0; 
		var lng = 0;
		var centre = new GLatLng(lat, lng);
		map.setCenter(centre, 2);
		draw(centre);
		map.addOverlay(new GMarker(centre));
	}
}

function loadAddresses(point) {
	$.get("area.php", { lat: point.lat().toFixed(5), lng: point.lng().toFixed(5), rad: document.getElementById("radius").value },
	  function(data){
		$("#emaillist").html(data);
	  });

}

function update() {
var point = new GLatLng(document.getElementById("lat").value, document.getElementById("lng").value);
draw(point);
map.addOverlay(new GMarker(point));
loadAddresses(point);
}

function clickFunc(marker, point) {   
	document.getElementById("lat").value = point.lat().toFixed(5);
	document.getElementById("lng").value = point.lng().toFixed(5);
	map.clearOverlays();	
	draw(point);	
	map.addOverlay(new GMarker(point));

	loadAddresses(point);

}//]]>
</script>

<script type="text/javascript">
//<![CDATA[

_mPreferMetric=true;


	//calling circle drawing function
	function draw(pnt){
	map.clearOverlays();
	bounds = new GLatLngBounds();
//	var givenRad =
	var givenRad = document.getElementById("radius").value*1;
	var givenQuality = 100;
	// var centre = pnt || map.getCenter()
	if(pnt) { 
		var centre = pnt;
	}
	else {
	var centre = new GLatLng(document.getElementById("lat").value, document.getElementById("lng").value);
	map.addOverlay(new GMarker(centre));

	}
	
	drawCircle(centre, givenRad, givenQuality);
	}

	////////////////////////// circle///////////////////////////////

	
function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa)
{
// Esa 2006
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

	//Loop 
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
	var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + 
	(radius/lngConv * Math.sin(i * Math.PI/180)));
	points.push(pint);
	bounds.extend(pint); //this is for fit function
	}
	points.push(points[0]); // Closes the circle, thanks Martin
	fillColor = fillColor||liColor||"#0055ff";
	liWidth = liWidth||2;
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
}
/////////////////////////////////////////////////////////////////////

//]]>

</script>

<table width="640" border="0">
<tr>
<td><p><strong>Radius:</strong> <input type="text" name="radius" id="radius" size="5" value="100" /> km</p></td><td align="right"><input type="button" onclick="update();" value="update" /></td></tr></table>

<div style="float: right;">
<p><strong>List of addresses:</strong></p>
<textarea id="emaillist" rows="25" cols="35">
</textarea>
</div>
<div id="map" style="width: 640px; height: 480px; position: relative; background-color: rgb(229, 227, 223);"></div>
<input type="hidden" id="lat" name="lat" style="width: 80px;" type="text">
<input type="hidden" id="lng" name="lng" style="width: 80px;" type="text">

<script type="text/javascript">
//<![CDATA[
load();
//]]>
</script>
</form>

<?

}

?>